package lance.honeydew.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import lance.honeydew.bean.ItemInformation;
import lance.honeydew.bean.PublishMessage;
import lance.honeydew.bean.UserMember;
import lance.honeydew.service.ManageService;

public class PublishMessageDAO extends RootDAO {
	
	private static final Logger logger = Logger.getLogger(PublishMessageDAO.class);
	
	/**
	 * findPublishMessageBySeqId
	 * @param seqId
	 * @return
	 */
	public PublishMessage findPublishMessageBySeqId(Long seqId) {
		
		PublishMessage result = null;
		
		try {
			
			result = (PublishMessage) this.findById(PublishMessage.class, PublishMessage.PROP_SEQID, seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * queryPublishMessageByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<PublishMessage> queryPublishMessageByQb(PublishMessage qb, int pageNum, int pageSize) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(PublishMessage.class);
		List<PublishMessage> list = null;
		
		try {
			
			if (StringUtils.isNotEmpty(qb.getPublishuser())) {
				criteria.add(Restrictions.eq(PublishMessage.PROP_PUBLISHUSER, qb.getPublishuser()));
			}
			
			if (qb.getPublishdate() != null) {
				criteria.add(Restrictions.eq(PublishMessage.PROP_PUBLISHDATE, qb.getPublishdate()));
			}
			
			if (qb.getFinaldate() != null) {
				criteria.add(Restrictions.eq(PublishMessage.PROP_FINALDATE, qb.getFinaldate()));
			}
			
			if (StringUtils.isNotEmpty(qb.getMessage())) {
				criteria.add(Restrictions.like(PublishMessage.PROP_MESSAGE, qb.getMessage(), MatchMode.ANYWHERE));
			}
			
			list = (List<PublishMessage>) this.getHibernateTemplate().
					findByCriteria(criteria);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * queryPublishMessageByExplain02
	 * @return
	 */
	public List<PublishMessage> queryPublishMessageByExplain02() {
		
		List<PublishMessage> result = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(PublishMessage.class);
		
		try {
			
			Date date = new Date();
			
			criteria.add(Restrictions.le(PublishMessage.PROP_PUBLISHDATE, date));
			criteria.add(Restrictions.ge(PublishMessage.PROP_FINALDATE, date));
			
			result = (List<PublishMessage>) this.getHibernateTemplate().
					findByCriteria(criteria);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		return result;
		
	}
}